package com.sunshine.springbootshirothymeleaf.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import com.sunshine.springbootshirothymeleaf.shiro.cache.RedisCacheManager;
import com.sunshine.springbootshirothymeleaf.shiro.realms.CustomerRealm;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 用来整合shiro框架相关的配置类
 */
@Configuration
public class ShiroConfig {

    //页面标签不起作用一定要记住加入方言处理 thymeleaf - shiro
    @Bean(name = "shiroDialect")
    public ShiroDialect shiroDialect(){
        return new ShiroDialect();
    }

    //1.创建shiroFilter  负责拦截所有请求
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultWebSecurityManager defaultWebSecurityManager){
        //创建shiro拦截器工厂对象
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        //给filter设置安全管理器
        shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);
        //配置系统受限资源
        //配置系统公共资源
        Map<String,String> map = new HashMap<String,String>();
        map.put("/login.html", "anon");
        map.put("/user/login","anon");//anon 设置为公共资源  放行资源放在下面
        map.put("/user/register", "anon");
        map.put("/user/registerView", "anon");
        map.put("/user/getImage", "anon");//验证码
        map.put("/**","authc");//authc 请求这个资源需要认证和授权
        //默认认证界面路径---当认证不通过时跳转
        shiroFilterFactoryBean.setLoginUrl("/user/loginView");
        //将配置好的权限注入到工厂对象中
        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
        return shiroFilterFactoryBean;
    }

    //2.创建安全管理器
    @Bean
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("getRealm") Realm realm){
        //创建web类型安全管理器
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        //给安全管理器设置realm
        defaultWebSecurityManager.setRealm(realm);

        return defaultWebSecurityManager;
    }

    //3.创建自定义realm
    @Bean
    public Realm getRealm(){
        //创建realm对象
        CustomerRealm customerRealm = new CustomerRealm();

        //设置hashed凭证匹配器
        HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher();
        //设置md5加密
        credentialsMatcher.setHashAlgorithmName("md5");
        //设置散列次数
        credentialsMatcher.setHashIterations(1024);
        //凭证匹配器注入到realm中
        customerRealm.setCredentialsMatcher(credentialsMatcher);

        //开启缓存管理器
        //customerRealm.setCacheManager(new EhCacheManager());//设置EhCache为缓存管理器
        customerRealm.setCacheManager(new RedisCacheManager());//设置Redis为缓存管理器
        customerRealm.setCachingEnabled(true);//开启全局缓存
        customerRealm.setAuthorizationCachingEnabled(true);//授权缓存
        customerRealm.setAuthorizationCacheName("authorizationCache");//授权缓存名字
        customerRealm.setAuthenticationCachingEnabled(true);//认证缓存
        customerRealm.setAuthenticationCacheName("authenticationCache");//认证缓存名字

        return customerRealm;
    }
}
